package com.tanhua.server.service;

import com.tanhua.autoconfig.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.MessageLikeVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.utils.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsService {

    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private StringRedisTemplate redisTemplate;

    public ResponseEntity queryComments(Integer page, Integer pagesize, String movementId) {
//        查询comment表 直接查出来的数据是app想要的吗
      PageResult pageResult = commentApi.queryComments(page,pagesize,movementId);
      List<Comment> items = (List<Comment>) pageResult.getItems();
//      把一个一个Comment转成CommentVo
        CommentVo commentVo = null;
        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : items) {
            commentVo = new CommentVo();
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());

            commentVo.setNickname(userInfo.getNickname());
            commentVo.setAvatar(userInfo.getAvatar());

            commentVo.setId(comment.getId().toString());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(new DateTime(comment.getCreated()).toString("HH:mm") );
            commentVo.setLikeCount(comment.getLikeCount()); //当前评论的点赞数据
            if(redisTemplate.hasKey("comment_like_"+ UserHolder.getUserId() +"_"+comment.getId().toString())){
                commentVo.setHasLiked(1); //当前登录人是否对这个评论点赞
            }else{
                commentVo.setHasLiked(0); //当前登录人是否对这个评论点赞
            }
            list.add(commentVo);
        }
        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);

    }

    public ResponseEntity saveComments(String movementId, String commentContent) {
//        1、保存comment数据
//        2、在圈子的评论数+1
//        3、返回这个圈子的评论数
        Comment comment = new Comment();
        comment.setUserId(UserHolder.getUserId());
        comment.setPublishId(new ObjectId(movementId)); //把字符串转成ObjectId
        comment.setCommentType(2); //评论类型，1-动态点赞，2-动态评论，3-动态喜欢 4-视频点赞  5-视频评论  6-评论点赞
        comment.setContent(commentContent); //这是个点赞的操作 没有描述
        Integer count = commentApi.saveComment(comment); // 返回这个圈子的点赞数

        return ResponseEntity.ok(null);


    }

    public ResponseEntity commentLike(String commentId) {
//        1、保存comment数据
//        2、在评论的点数+1
//        3、返回这个评论的点赞数
        Comment comment = new Comment();
        comment.setUserId(UserHolder.getUserId());
        comment.setPublishId(new ObjectId(commentId)); //把字符串转成ObjectId
        comment.setCommentType(6); //评论类型，1-动态点赞，2-动态评论，3-动态喜欢 4-视频点赞  5-视频评论  6-评论点赞
        comment.setContent(""); //这是个点赞的操作 没有描述
        Integer count = commentApi.saveComment(comment); // 返回这个评论的点赞数

        redisTemplate.boundValueOps("comment_like_"+ UserHolder.getUserId() +"_"+commentId).set("");

        return ResponseEntity.ok(count);
    }

    public ResponseEntity commentDislike(String commentId) {
        Comment comment = new Comment();
        comment.setUserId(UserHolder.getUserId());
        comment.setPublishId(new ObjectId(commentId)); //把字符串转成ObjectId
        comment.setCommentType(6); //评论类型，1-动态点赞，2-动态评论，3-动态喜欢 4-视频点赞  5-视频评论  6-评论点赞
        comment.setContent(""); //这是个点赞的操作 没有描述

        Integer count = commentApi.deleteComment(comment);

        redisTemplate.delete("comment_like_"+ UserHolder.getUserId() +"_"+commentId);


        return ResponseEntity.ok(count);
    }

    public ResponseEntity queryCommentPeople(Integer page, Integer pagesize, int commentType) {
     PageResult pageResult =    commentApi.queryCommentPeople(page,pagesize,commentType,UserHolder.getUserId());
     List<Comment> items = (List<Comment>) pageResult.getItems();
//     把items中的一个一个的Comment转成MessageLikeVo
        List<MessageLikeVo> list = new ArrayList<>();
        for (Comment item : items) {
            MessageLikeVo messageLikeVo = new MessageLikeVo();
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            messageLikeVo.setId(userInfo.getId().toString());
            messageLikeVo.setAvatar(userInfo.getAvatar());
            messageLikeVo.setNickname(userInfo.getNickname());
            messageLikeVo.setCreateDate( new DateTime(item.getCreated()).toString("yyyy-MM-dd HH:mm") ); // yyyy-MM-dd HH:mm
            list.add(messageLikeVo);
        }

        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);

    }


    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity addContacts(Long friendId) {
//        1、把两个人的好友关系 添加到Friend表中
//        当前登录人是2号用   friendId是105号用户
        friendApi.saveFriend(UserHolder.getUserId(),friendId);
//        2、把两个人的好友关系登记到环信上
        huanXinTemplate.contactUsers(UserHolder.getUserId(),friendId);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity queryContacts(Integer page, Integer pagesize) {

//        查询当前登录人的好友
     PageResult pageResult =   friendApi.queryContactsByUserId(UserHolder.getUserId(),page,pagesize);
        List<Friend> items = (List<Friend>) pageResult.getItems();
//        把一个一个的 Friend转成  ContactVo
        ContactVo contactVo = null;
        UserInfo userInfo = null;
        List<ContactVo> list = new ArrayList<>();
        for (Friend friend : items) {
            userInfo = userInfoApi.findById(friend.getFriendId());
//            查询一个一个的UserInfo
            contactVo = new ContactVo();
            BeanUtils.copyProperties(userInfo,contactVo);
            contactVo.setUserId(userInfo.getId().toString());
//            userInfo中的city 北京市-北京城区-东城区
            if(userInfo.getCity()!=null){
                contactVo.setCity(userInfo.getCity().split("-")[0]);
            }
            list.add(contactVo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);

    }
}
